<?php

/*
 * tiers/edit.phtml
 */
?>
<?php

$title = "Nouveau tiers";
$this->headTitle($title);

$dWk = array('lundi', 'mardi', 'mercredi', 'jeudi', 'vendredi', 'samedi', 'dimanche') ;
?>

<div class='content-head'>
    <h3>
	<?php echo $title; ?>	
    </h3>
    
</div>

<div class='content-body'>
    <div class="content-body tiers-border" style='min-height: 500px;'> 
        <form id='form-store' method='post' action='<?php echo $this->url('RHX/default', array('controller' => 'tiers', 'action' => 'addNew') );?>' style='display:none;'>            
            <?php $fieldString = array('libelle', 'contact', 'adresse', 'siret', 'website', 'phone', 'fax', 'mail', 'photo', 'rib' );?>
            
            <?php foreach ($fieldString as $f) : ?>     
                <input type='hidden' id='store-<?php echo $f;?>' name='store[<?php echo $f;?>]' value='' >
            <?php endforeach;?>
            <input type='hidden' id='store-photo_extension' name='store[photo_extension]' value='' >
	    <input type='hidden' id='store-rib_extension' name='store[rib_extension]' value='' >
	    
            <?php $dWk = array('lundi', 'mardi', 'mercredi', 'jeudi', 'vendredi', 'samedi', 'dimanche') ; ?>
            <?php foreach ($dWk as $day) :  
                    $day_ouv = $day.'_ouverture';
                    $day_fer = $day.'_fermeture';
            ?>
                <input type='hidden' id='store-<?php echo $day;?>' name='store[<?php echo $day;?>]' value='0' >
                <input type='hidden' id='store-<?php echo $day_ouv;?>' name='store[<?php echo $day_ouv;?>]' value='00:00' >
                <input type='hidden' id='store-<?php echo $day_fer;?>' name='store[<?php echo $day_fer;?>]' value='00:00' >
            <?php endforeach;?>
            
        </form>   
        <div class="tiers-top ">        
             <div class="inf-tiers inf-tiers-grise" style="margin-top: 20px;"> 
                 Libellé : <a id= 'tiers-libelle' href='#' data-type='text'  data-title='Edition du libellé'> </a>
             </div>
         </div>
        
        <div class='tiers-left '>
             <div class="cnt-photo-tiers" >
                    <a href='javascript: void(0);' class='cnt-img-tiers' id='link-uploadPhoto'>
                        <?php
                        $basePath	 =  $this->basePath(); 
                        $photoUrl = $basePath .  '/img/rhx/tiers-default.jpg'; 
                        ?>

                    <img id="tiers-photo" src="<?php echo  $photoUrl ;?>" width="320" height="auto">
                    </a>
                    <a href="#" id="btn-uploadPhotoTiers"  class='btn btn-info btn-small' style="display:none; z-index:55555;">
                            Télécharger photo
                    </a>
             </div>  
            
            <div class="inf-tiers inf-tiers-pink">
                Contact : <a id= 'tiers-contact' href='#' class=''  data-type="text"data-title='Edition du contact'> </a></div>     
                
                <div class="inf-tiers inf-tiers-pink">
                Siret : <a id= 'tiers-siret' href='#' class=''  data-type="text" data-title='Edition du siret'> </a></div>     
                
                <div class="inf-tiers inf-tiers-pink">
                Site web : <a id= 'tiers-website' href='#' class=''  data-type="text" data-title='Edition du site web'> </a></div>     
                
        </div>
        
        <div class='tiers-right'>
            <div class="inf-tiers inf-tiers-blue">
                Adresse : <a id= 'tiers-adresse' href='#' class=''  data-type="text" data-title="Edition de l'adresse"></a>
            </div>     
                
            <div class="inf-tiers inf-tiers-blue">
                Téléphone : <a id= 'tiers-phone' href='#' class='' data-type="text" data-title="Edition du téléphone" ></a>
            </div>
            <div class="inf-tiers inf-tiers-blue">
                Fax : <a id= 'tiers-fax' href='#' class=''  data-type="text" data-title="Edition du Fax"></a>
            </div>
            <div class="inf-tiers inf-tiers-blue">
                Mail : <a id= 'tiers-mail' href='#' class=''  data-type="text" data-title="Edition de l'adresse e-mail"></a>
            </div>
	    
	    <div class='cnt-inf-tiers cnt-inf-tiers-rib_top clear-auto' style='margin-top: 20px;'>            
		    <div class='inf-tiers inf-tiers-yellow ' > RIB :   			
			<span id='label-uploadRib' style='display: none;'></span>
			<a href='javascript: void(0);' class='bt btn btn-info btn-small' id= 'link-uploadRib' >Télécharger</a>			
		    </div>     


	       </div>
            
            <div class='cnt-inf-site clear-auto' style='margin-top: 20px;'>                
           
                <div class='inf-site inf-site-white inf-site-border-blue' style='margin-bottom: 6px;'> 
                         Horaire du Tiers 
                </div>
                <table id='table-tiers-horaire' class='table table-striped '>
                 <thead>
                  <tr>
                      <?php foreach ($dWk as $d ): ?>
                      <td width='9%'>
                          <?php echo strtoupper($d[0]);?> 

                      </td>
                      <?php endforeach ;?>


                  </tr>
                 </thead>

                  <tbody>
                       <tr>
                         <?php foreach ($dWk as $d ): ?>
                         <td style=' padding-top: 0 ;padding-bottom: 0 ;'>

                             <input class="checkbox checkbox-open-state" type="checkbox" value=''  data-day='<?php echo $d;?>' >
                         </td>
                         <?php endforeach ;?>


                     </tr>
                      <tr> 
                          <?php foreach ($dWk as $d ): ?>
                             <?php $d_ouv = $d. '_ouverture' ;?>

                             <td>
                                 <a class="hr-ouverture hr-ouverture-<?php echo $d;?>" data-store='<?php echo $d; ?>_ouverture' data-title="Heure d'ouverture le <?php echo $d;?>" data-viewformat="HH\h mm\m" data-format="HH:mm" data-template="HH:mm" data-type="combodate" href="#" >00h 00m</a>
                             </td>

                          <?php endforeach ;?>
                      </tr>
                      <tr> 
                           <?php foreach ($dWk as $d ): ?>
                             <?php $d_fer = $d. '_fermeture' ;?>
                             <td>
                                 <a class="hr-fermeture hr-fermeture-<?php echo $d;?>" data-store='<?php echo $d; ?>_fermeture' data-title="Heure de fermeture le <?php echo $d;?>"  data-viewformat="HH\h mm\m" data-format="HH:mm" data-template="HH:mm" data-type="combodate" href="#">00h 00m
                                 </a>
                             </td>
                          <?php endforeach ;?>
                      </tr>

                  </tbody>      
                 </table>



		<!--
            <div  class='cnt-inf-tiers cnt-inf-tiers-btns clear-auto'  >           
                <a href='javascript: void(0);' class='bt bt-show ' id='link-obligatoire'>Affichage obligatoire</a>                
            </div>
		-->

        </div>
        </div>
    </div>
</div>

<div class="content-head">
    <h3>&nbsp;</h3>
    <div class="toolbar">
	<button class='btn btn-success' id='finish-creating'>Créer le Nouveau Tiers</button>
	<button class='btn btn-inverse' onclick="location.href = '<?php echo $this->url('RHX/default', array('controller'	 => 'tiers')) ?>';">Annuler et revenir à la liste des Tiers</button>
    </div>
</div>

<!-- Script -->
<script type='text/javascript'>
    
    var JsTiersAdd = (function(){
        var updateStore = function (idStore, v){
                       $('#store-'+ idStore).val($.trim(v));}
            ,isEmpty = function(selector){
                return $.trim($(selector).val()) === '';
            },
            isFloat = function(val){
                v = $.trim(val);
                patFloat = new RegExp(/^[0-9]*[.]{0,1}[0-9]+$/);

                return patFloat.test(v);                     
            }
            ;
            
        return {
            init : function(){                
                JsTiersAdd.initAdding();
		JsTiersAdd.initErgo();   
                JsTiersAdd.initHoraire();
                JsTiersAdd.add();
		JsTiersAdd.initUpl();
            },            
            initAdding : function(){console.log('init adding');
                
                $('#tiers-libelle' ).editable({
                    title: 'Edition du libellé',
                    url: '<?php echo $this->url('RHX/default', array('controller'	 => 'tiers', 'action'	 => 'editData')) ?>',
                    validate : function(val){
                        if($.trim(val) === '')  
                            return 'Le libellé ne peut être vide';
                        updateStore('libelle' , val );
                    }
                });
                
                 <?php $fieldStringJs = array('contact', 'adresse', 'siret', 'website', 'phone', 'fax', 'mail');?>
                     
                 fieldStringJS = ['<?php echo implode("', '", $fieldStringJs)    ?>'];
                 $.each(fieldStringJS , function (k, f){
                     
                     $('#tiers-'+ f ).editable({                       
                        url:  "<?php echo $this->url('RHX/default', array('controller'	 => 'tiers', 'action'	 => 'editData')) ;?>",
                        validate: function(val){  
                            updateStore( f, val );                        
                        }
                     });
                 });
                 console.log(fieldStringJS);
               
                
            },
            initHoraire : function(){
                
                // horaire tiers
                    $('.hr-ouverture').each(function(){
                        $(this).editable({
                             /*placement: 'right',*/
                             combodate: {
                                 firstItem: 'name',
                                 minuteStep: 5
                             },                
                             url: "<?php echo $this->url('RHX/default', array('controller'	 => 'tiers', 'action'	 => 'editData')) ?>",
                             validate: function(val){                                 
                                 new_hour = val.hour()+ ':'+ val.minute();                                     
                                 console.log(new_hour);
                                 updateStore( $(this).data('store'), new_hour );                                  
                             }
                         });           
                    });
                     $('.hr-fermeture').each(function(){
                        $(this).editable({
                             /*placement: 'right',*/
                             combodate: {
                                 firstItem: 'name',
                                 minuteStep: 5
                             },
                             url: "<?php echo $this->url('RHX/default', array('controller'	 => 'tiers', 'action'	 => 'editData')) ?>",
                             validate: function(val){                                 
                                 new_hour = val.hour()+ ':'+ val.minute()+ '';                                 
                                 console.log(new_hour);
                                 updateStore( $(this).data('store'), new_hour ); 
                             }
                        });           
                    });
                    
                    $('.checkbox-open-state').each(function(){
                            var $this = $(this), 
                                initThis = function(){
                                     var day = $this.data('day'),
                                         $hrOuv = $('.hr-ouverture-'+ day ),
                                         $hrFer = $('.hr-fermeture-'+ day );
                                    
                                    $hrOuv.editable('disable');
                                    $hrFer.editable('disable');
                                    
                                    $hrOuv.html('...'); 
                                    $hrFer.html('...');                                    
                                };
                                
                            // design bootstrap
                            $this.checkbox();   
                            
                            // init checkbox                             
                            initThis();
                            //init onchange
                            $this.on( 'change', function(){
                                var $its = $(this),
                                    day = $its.data('day');
                                    $hrOuv = $('.hr-ouverture-'+ day),
                                    $hrFer = $('.hr-fermeture-'+ day), 
                                    strStoreState = day,
                                    strStoreOuv = $hrOuv.data('store'),
                                    strStoreFer = $hrFer.data('store');
                                    
                                    isChecked = $this.is(':checked') ? 1 : 0;
                                
                                //enable-disable
                                    $hrOuv.editable(isChecked ? 'enable' :'disable');
                                    $hrFer.editable(isChecked ? 'enable' :'disable');
                                
                                //storage
                                
                                updateStore(strStoreState ,  isChecked );                                 
                                if(isChecked ){
                                    //update store ouv fer >>> 7>23
                                    $hrOuv.html('07h 00m');
                                    $hrFer.html('23h00m');
                                    updateStore(strStoreOuv ,  '07:00'); 
                                    updateStore(strStoreFer ,  '23:00'); 
                                    //...
                                    //enable editable
                                }
                                else{
                                    //update store ouv fer >>> 0>0
                                    $hrOuv.html('...');
                                    $hrFer.html('...');
                                    updateStore(strStoreOuv ,  '00:00'); 
                                    updateStore(strStoreFer ,  '00:00'); 
                                    
                                    //disable editable
                                }
                             });  
                        });                          
            },
            add : function(){
                $('#finish-creating').on('click', function(){
                    var $form = $('form#form-store'),
                        verifyData = function(){
                            var res = {ok:true, err: ''},
                                err = '';
                            
                            if($('#store-libelle').val() === ''){ 
                                err = err + 'Le libelle ne peut être vide <br>';
                                res.ok = false;
                            }
                            if(false){
                                err += 'Other err!';
                                res.ok = false;
                            }
                            res.err = err;
                            
                            return res;
                        };
                    
                    validity = verifyData();                    
                    if(!validity.ok){
                        showError(validity.err);
                        return 0;
                    }
                    
                    $form.submit();
                });
            }, 
	    initErgo : function(){
		$('.cnt-photo-tiers').hover(function(e){                    
                     $('#btn-uploadPhotoTiers').css('display', '');
                }).mouseleave(function(e){                    
                     $('#btn-uploadPhotoTiers').css('display', 'none');
                });
	    },
	    initUpl : function (){
                // uploading photo
                    $(['#link-uploadPhoto', '#btn-uploadPhotoTiers']).each(function (k, id){
                        $(id).on ('click', function(e){
                           e.preventDefault();
                           uploadWindow = window.open('<?php echo $this->url('RHX/default', array('controller'	 => 'tiers', 'action'	 => 'uploadPhoto')) ?>', 'UploadWindow', 'menubar=no, scrollbars=no, top=100, left=100, width=500, height=400');
                        });
                    });
                
		// upload rib                
                $('#link-uploadRib').on('click', function(e){
                        e.preventDefault();
                        
                        uploadWindow = window.open('<?php echo $this->url('RHX/default', array('controller'	 => 'tiers', 'action'	 => 'uploadRib')) ?>', 'UploadWindow', 'menubar=no, scrollbars=no, top=100, left=100, width=500, height=400');
                     });
		$('#link-obligatoire').on('click', function(e){
                        e.preventDefault();
                        alert('En cours de développement');
                        
                     });
                    	     
            }
        } 
    } )();
    
    $(document).bind('ready', JsTiersAdd.init);
</script>
